package com.zsy.petshop_01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zsy.petshop_01.common.result.ResponseResult;
import com.zsy.petshop_01.dao.mapper.PetUserMapper;
import com.zsy.petshop_01.manager.UserManager;
import com.zsy.petshop_01.model.entity.LoginInfo;
import com.zsy.petshop_01.model.entity.User;
import com.zsy.petshop_01.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    PetUserMapper petUserMapper;
    @Autowired
    UserManager userManager;

    @Override
    public ResponseResult<?> Login(LoginInfo loginInfo) {
        try {
            User petUser = userManager.login(loginInfo);
            if (petUser == null) {
                return ResponseResult.fail("用户名不存在");
            }
            if (!petUser.getPassword().equals(loginInfo.getPassword())) {
                return ResponseResult.fail("密码错误");
            }
            return ResponseResult.success("登录成功", petUser.getId());
        } catch (Exception e) {
            log.error("登录失败" + e.getMessage());
            return ResponseResult.fail("系统出错" + e.getMessage());
        }
    }

    @Override
    public ResponseResult<?> Register(LoginInfo loginInfo) {
        try {
            User petUser = userManager.login(loginInfo);
            if (petUser != null) {
                return ResponseResult.fail("用户名已存在");
            }
            User petUser1 = new User();
            petUser1.setName(loginInfo.getUsername());
            petUser1.setPassword(loginInfo.getPassword());
            userManager.insert(petUser1);
            return ResponseResult.success("注册成功");
        } catch (Exception e) {
            log.error("注册失败" + e.getMessage());
            return ResponseResult.fail("系统出错" + e.getMessage());
        }
    }

    @Override
    public ResponseResult<?> selectAllUser() {
        List<User> users = userManager.selectAllUser();
        return ResponseResult.success(users);
    }

    @Override
    public ResponseResult<?> delateByName(LoginInfo loginInfo) {
        try {
            userManager.deleteByName(loginInfo.getUsername());
            return ResponseResult.success("删除成功");
        } catch (Exception e) {
            log.error("删除失败" + e.getMessage());
            return ResponseResult.fail("系统出错" + e.getMessage());
        }
    }

    @Override
    public ResponseResult<?> setVIP(LoginInfo loginInfo) {
        try {
            User petUser = userManager.login(loginInfo);
            if(petUser.getVip() == 1)
                petUser.setVip(0);
            else
                petUser.setVip(1);
            userManager.updateById(petUser);
            return ResponseResult.success("设置成功");
        } catch (Exception e) {
            log.error("设置失败" + e.getMessage());
            return ResponseResult.fail("系统出错" + e.getMessage());
        }
    }

    @Override
    public ResponseResult<?> verifyVIP(HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getHeader("user_id"));
        User petUser = userManager.selectById(userId);
        return petUser.getVip() == 1 ? ResponseResult.success("是VIP") : ResponseResult.fail("不是VIP");
    }

    @Override
    public ResponseResult<?> verifyOwner(HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getHeader("user_id"));
        User petUser = userManager.selectById(userId);
        return petUser.getOwner() == 1 ? ResponseResult.success("是VIP") : ResponseResult.fail("不是VIP");
    }
}
